﻿#region Using Statements
using System;
using System.Collections.Generic;
using GameBase.Cameras;
using GameBase.Screens;
using GameBase.Sprites;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace StarFox.ScreenComponents
{
    /// <summary>
    /// Basic properties needed to implement any head-up display bar.
    /// </summary>
    internal class HudBar : IDisposable
    {
        #region Fields

        Screen screen;
        Rectangle postRect, negtRec;

        readonly Texture2D postTex, negtTex;
        readonly Color shadeColor = new Color(51, 51, 51, 191);

        #endregion

        #region Bar Color

        /// <summary>
        /// Sets the color of the head-up display bar.
        /// </summary>
        public Color Color
        {
            private get;
            set;
        }

        #endregion

        #region Bar Value

        /// <summary>
        /// Gets or sets the value of the head-up display bar.
        /// </summary>
        public float Value
        {
            get;
            set;
        }

        #endregion

        #region Bar Dimensions

        // backing fields
        int width, height;

        /// <summary>
        /// Gets or sets the width of the head-up display bar.
        /// </summary>
        public int Width
        {
            get { return width; }
            set { width = value; }
        }

        /// <summary>
        /// Gets or sets the height of the head-up display bar.
        /// </summary>
        public int Height
        {
            get { return height; }
            set { height = value; }
        }

        #endregion

        #region Bar Position

        // backing fields
        int x, y;

        /// <summary>
        /// Gets or sets the x-coordinate of the head-up display bar.
        /// </summary>
        public int X
        {
            get { return x; }
            set { x = value; }
        }

        /// <summary>
        /// Gets or sets the y-coordinate of the head-up display bar.
        /// </summary>
        public int Y
        {
            get { return y; }
            set { y = value; }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of this class.
        /// </summary>
        /// <param name="screen">The Screen that the head-up display is associated to.</param>
        /// <param name="width">Width of the head-up display bar.</param>
        /// <param name="height">Height of the head-up display bar.</param>
        public HudBar(Screen screen, int width, int height)
        {
            this.screen = screen;
            this.width = width;
            this.height = height;

            Value = 1.0f;

            // positive part of the HUD bar
            postTex = new Texture2D(this.screen.GraphicsDevice, 1, 1);
            postTex.SetData(new[] { Color.White });

            // negative part of the HUD bar
            negtTex = new Texture2D(this.screen.GraphicsDevice, 1, 1);
            negtTex.SetData(new[] { shadeColor });
        }

        #endregion

        #region Dispose

        // dispose flag
        bool isDisposed = false;

        /// <summary>
        /// Immediately releases all resources used by this object.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
        /// </summary>
        ~HudBar()
        {
            Dispose(false);
        }

        /// <summary>
        /// Releases the unmanaged resources used by this object and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!isDisposed)
            {
                if (disposing)
                {
                    if (postTex != null) { postTex.Dispose(); }
                    if (negtTex != null) { negtTex.Dispose(); }
                }

                isDisposed = true;
            }
        }

        #endregion

        #region Draw

        /// <summary>
        /// Draws the head-up display bar.
        /// </summary>
        public void Draw()
        {
            // positive part of the HUD bar
            postRect = new Rectangle(x, y, (int)(width * Value), height);
            screen.SpriteBatch.Draw(postTex, postRect, Color);

            // negative part of the HUD bar
            negtRec = new Rectangle(x + (int)(width * Value), y, (int)(width * (1 - Value)), height);
            screen.SpriteBatch.Draw(negtTex, negtRec, Color);
        }

        #endregion
    }
}
